package com.fafu.service.Impl;

import com.fafu.dao.mapper.ImageMapper;
import com.fafu.dao.mapper.StudentrepairMapper;
import com.fafu.dao.mapper.WorkerMapper;
import com.fafu.model.Image;
import com.fafu.model.Managerepair;
import com.fafu.model.Student;
import com.fafu.model.Worker;
import com.fafu.pojo.AppPojo4Item;
import com.fafu.pojo.AppPojo4list;
import com.fafu.pojo.WorkerHistoryPojo;
import com.fafu.service.WorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import util.Dictionary;
import util.JsonRsp;
import util.MD5Utils;
import util.MyMap;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static util.Dictionary.CODE.IDEN_EXPIRED;
import static util.Dictionary.CODE.SUCCESS;
import static util.Dictionary.TABLE.TABLE_STUDENT_REPAIR;

@Service
public class WorkerServiceImpl implements WorkerService {
    @Autowired
    WorkerMapper workerMapper;
    @Autowired
    StudentrepairMapper studentrepairMapper;
    @Autowired
    ImageMapper imageMapper;
    @Override
    public List<Worker> searchWorkerList(Map<String, Object> paramMap) {
        String key = (String) paramMap.get("key");
        Example workerException = new Example(Worker.class);
        Example.Criteria workerCriteria = workerException.createCriteria();
        workerCriteria.orLike("name","%"+key+"%").orLike("work","%"+key+"%");
        List<Worker> reList = workerMapper.selectByExample(workerException);
        return reList;
    }

    @Override
    public Worker searchWorkerByid(Integer id) {
        return workerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<WorkerHistoryPojo> getHistorylist(Integer id) {
        return workerMapper.getWorkerHistorylist(new MyMap().put("id",id).toMap());
    }

    @Override
    public Map<String, Object> regiser(Worker worker) {
        String iden = worker.getPhone();
        Example example = new Example(Worker.class);
        example.and().andEqualTo("phone",iden);
        List<Worker> workers = workerMapper.selectByExample(example);
        if(workers != null && workers.size() > 0)
            return new MyMap().put("code", IDEN_EXPIRED).put("msg","账号存在").toMap();
        String salt = MD5Utils.createSalt();
        String pwd = MD5Utils.passwordAddSalt(worker.getPassword(),salt);
        worker.setPassword(pwd);
        worker.setSalt(salt);
        worker.setTime(0);
        worker.setIsused(1);
        worker.setAddTime(System.currentTimeMillis());
        workerMapper.insertSelective(worker);
        return new MyMap().put("code", SUCCESS).toMap();
    }

    @Override
    public Map<String, Object> login(Map<String, Object> paramMap) {
        String iden = (String) paramMap.get("workerId");
        String pwd = (String) paramMap.get("password");
        Example example = new Example(Worker.class);
        example.and().andEqualTo("phone",iden);
        List<Worker> workers = workerMapper.selectByExample(example);
        if(workers == null || workers.size() == 0)
            return new MyMap().put("code", Dictionary.CODE.IDEN_ERROR).put("msg","账号不存在").toMap();
        if(!workers.get(0).getPassword().equals(MD5Utils.passwordAddSalt(pwd,workers.get(0).getSalt())))
            return new MyMap().put("code", Dictionary.CODE.PASSWORD_ERROR).put("msg","密码错误").toMap();

        return new MyMap().put("code", Dictionary.CODE.SUCCESS)
                .put("msg",workers.get(0).getWork())
                .put("iden",workers.get(0).getPhone())
                .put("id",workers.get(0).getId())
                .put("isused",workers.get(0).getIsused())
                .put("name",workers.get(0).getName()).toMap();

    }

    @Override
    public Map<String, Object> changePwd(Map<String, Object> paramMap) {
        String iden = (String) paramMap.get("workerId");
        String oldpwd = (String) paramMap.get("oldpwd");
        String pwd = (String) paramMap.get("newpwd");
        Example example = new Example(Worker.class);
        example.and().andEqualTo("phone",iden);
        List<Worker> workers = workerMapper.selectByExample(example);
        if(workers == null || workers.size() == 0)//判断账号
            return new MyMap().put("code", Dictionary.CODE.IDEN_ERROR).put("msg","账号不存在").toMap();
        if(!workers.get(0).getPassword().equals(MD5Utils.passwordAddSalt(oldpwd,workers.get(0).getSalt())))//判断密码
            return new MyMap().put("code", Dictionary.CODE.PASSWORD_ERROR).put("msg","密码错误").toMap();
        String salt = MD5Utils.createSalt();
        String pwdSalt = MD5Utils.passwordAddSalt(pwd,salt);
        Worker worker = workers.get(0);
        worker.setPassword(pwdSalt);
        worker.setSalt(salt);
        workerMapper.updateByPrimaryKeySelective(worker);
        return new MyMap().put("code", Dictionary.CODE.SUCCESS).put("msg","密码修改成功").toMap();
    }

    @Override
    public Map<String, Object> selectRecordItem(Map<String, Object> paramMap) {
        AppPojo4Item record ;
        record = studentrepairMapper.appSelectRecordItem(paramMap);
        Example example = new Example(Image.class);
        example.and().andEqualTo("repairid",record.getId()).andEqualTo("tableIden",TABLE_STUDENT_REPAIR);
        List<Image> imagesList = imageMapper.selectByExample(example);
        List<String> urlList = new ArrayList<>();
        for(Image data:imagesList){
            urlList.add(data.getUrl());
        }
        record.setPicList(urlList);
        record.setTitle(Dictionary.TITLE.getTitle(record.getState()));
        return new MyMap().put("data",record).toMap();
    }

    @Override
    public Map<String, Object> selectRecordList(Map<String, Object> paramMap) {
        /**
         * 修改状态值为1；
         * 查询结果返回
         *      返回：AppPojo4list
         */
        Integer workerId = (Integer) paramMap.get("workerId");
        workerMapper.changeState(new MyMap().put("workerId",workerId).toMap());
        List<AppPojo4list> reList = workerMapper.appSelectRecordList(
                new MyMap().put("workerId",workerId).toMap());
        Example example;
        for(AppPojo4list data:reList){
            example = new Example(Image.class);
            example.and().andEqualTo("tableIden",TABLE_STUDENT_REPAIR).andEqualTo("repairid",data.getId());
            List<Image> images = imageMapper.selectByExample(example);
            if(images != null && images.size()>0)
                data.setPic(images.get(0).getUrl());
        }
        return new MyMap().put("data",reList).toMap();
    }

    @Override
    public Map<String, Object> msgChange(Map<String, Object> paramMap) {
        Integer id = (Integer) paramMap.get("workerId");
        Worker worker = workerMapper.selectByPrimaryKey(id);
        if(worker == null)
            return new MyMap().put("code", Dictionary.CODE.IDEN_ERROR).put("msg","账号不存在").toMap();
        worker.setWork((String) paramMap.get("msg"));
        workerMapper.updateByPrimaryKeySelective(worker);
        return new MyMap().put("code", Dictionary.CODE.SUCCESS).toMap();
    }
}
